import { Injectable, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { SettingsService, TitleService, ALAIN_I18N_TOKEN, } from '@delon/theme';
import { TranslateService } from '@ngx-translate/core';
import { I18NService } from '@core/i18n/i18n.service';
import { environment } from '@env/environment';

/**
 * 用于应用启动时
 * 一般用来获取应用所需要的基础数据等
 */
@Injectable()
export class StartupService {

  constructor(
    private translSrv: TranslateService,
    @Inject(ALAIN_I18N_TOKEN) private i18nSrv: I18NService,
    private setSrv: SettingsService,
    private titleSrv: TitleService,
    private http: HttpClient
  ) { }

  public load(): Promise<any> {
    this.setSrv.setApp(environment.appData);
    this.titleSrv.suffix = environment.appData.name;

    const apiLang: string = `assets/i18n/${this.i18nSrv.defaultLang}.json`;
    return new Promise(resolve => {
      this.http.get(apiLang).subscribe(
        langData => {
          this.translSrv.setTranslation(this.i18nSrv.defaultLang, langData);
          this.translSrv.setDefaultLang(this.i18nSrv.defaultLang);
        },
        resolve,
        resolve
      );
    });
  }

}
